﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ITeBooks.API.Library.Converter;
using ITeBooks.API.Library.Model;
using ITeBooks.API.Library.Util;
using System.Configuration;
using System.IO;
using ITeBooks.API.Library.Exception;

namespace ITeBooks.API.Library.Test
{
    [TestClass]
    public class JSONEBookConverterTest
    {
        [TestMethod]
        public void FindBooks()
        {
            EBook[] ebooks = new EBook[2];

            EBook ebook1 = new EBook(1025718732, "Microsoft SQL Azure", "Subitle", String.Empty, "http://s.it-ebooks-api.info/8/developing_microsoft_sharepoint_applications_using_windows_azure.jpg", 9780735656628);
            EBook ebook2 = new EBook(1025718738, "Microsoft SQL Azure2", "Subitle", String.Empty, "http://s.it-ebooks-api.info/8/developing_microsoft_sharepoint_applications_using_windows_azure.jpg", 9780735656627);
            ebooks[0] = ebook1;
            ebooks[1] = ebook2;
            QueryResult actual = new QueryResult("0", 0.0043, 1, 2, ebooks);

            //TODO: renomear variaveis
            string a = System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.FullName + "\\TestFiles\\findBooks.json";

            FileStream fs = new FileStream(a, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            JSONEBookConverter b = new JSONEBookConverter(sr.ReadToEnd(), false);
            QueryResult expected = b.Find();

            Assert.AreEqual<QueryResult>(actual, expected);
        }

        [TestMethod]
        public void FindBookByID()
        {
            EBook ebook1 = new EBook(1025718732, "Microsoft SQL Azure", "Subitle", "Develop large scale", "http://s.it-ebooks-api.info/8/developing_microsoft_sharepoint_applications_using_windows_azure.jpg", 9781849680806, new string[] { "Jayaram Krishnaswamy" }, 2010, 420, "Packt Publishing", "http://filepi.com/i/kAqDyoo");
            QueryResult actual = new QueryResult("0", 0.0006, ebook1);

            string a = System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.FullName + "\\TestFiles\\findBookByID.json";

            FileStream fs = new FileStream(a, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            JSONEBookConverter b = new JSONEBookConverter(sr.ReadToEnd(), false);
            QueryResult expected = b.FindByID();

            Assert.AreEqual<QueryResult>(actual, expected);
        }

        [TestMethod]
        [ExpectedException(typeof(EBookQueryException), "Query not found!")]
        public void FindBooksQueryException_NullQuery()
        {
            JSONEBookConverter b = new JSONEBookConverter(null);
            b.Find();
        }

        [TestMethod]
        [ExpectedException(typeof(EBookQueryException), "Query not found!")]
        public void FindBooksQueryException_NoParameters()
        {
            JSONEBookConverter b = new JSONEBookConverter(new EBookQuery());
            b.Find();
        }

        [TestMethod]
        [ExpectedException(typeof(EBookException), "Book {id} not found!")]
        public void BookNotFoundException()
        {
            string a = System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.FullName + "\\TestFiles\\bookNotFound.json";

            FileStream fs = new FileStream(a, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            JSONEBookConverter b = new JSONEBookConverter(sr.ReadToEnd(), false);
            QueryResult expected = b.FindByID();
        }

        public void IsInUnitTest()
        {
            Assert.IsTrue(UnitTestDetector.IsInUnitTest,
                "Should detect that we are running inside a unit test."); // lol
        }
    }
}
